package com.qs.eggs.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.qs.eggs.Constants;
import com.qs.eggs.R;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


public class ProductAdapter extends BaseAdapter {
    List<Map<String, Object>> giflist=new ArrayList<>();
    private Context context;
    private  String imageUrl;

    public ProductAdapter(Context context, List<Map<String, Object>> giflist) {
        super();
        this.context = context;
        this.giflist = giflist;
    }
    @Override
    public int getCount() {
        return giflist.size();
    }


    @Override
    public Object getItem(int position) {
        return giflist.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
       ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(
                    R.layout.buy, null);
            holder.name= (TextView) convertView.findViewById(R.id.name);
            holder.price= (TextView) convertView.findViewById(R.id.jiage);
            holder.image = (ImageView) convertView.findViewById(R.id.imageView);
            convertView.setTag(holder);
        } else {
            // 取出holder
            holder = (ViewHolder) convertView.getTag();
        }
        imageUrl= Constants.URL_BASE+"productimages/"+giflist.get(position).get("autoname");
        Picasso.with(context).load(imageUrl).into(holder.image);
        holder.name.setText(giflist.get(position).get("proname").toString());
        holder.price.setText("¥"+giflist.get(position).get("price")+"");
        return convertView;
    }

    public class ViewHolder {

        TextView name,price;
        ImageView image;


    }
}
